package com.dk.demo;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpClientDemo {
    /**
     * get请求
     */
    @Test
    public void demo01() {
        /**
         * - 1.创建一个HttpClient对象
         * - 2.创建一个Get请求 ,在括号里写上要调用的第三方的接口
         * - 3.使用HttpClient来执行Get请求, 获取response
         * - 4.判断响应状态码是不是200，获取响应数据实体对象
         * - 5.处理response
         * - 6.关闭HttpClient
         */
//        1.创建一个HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
//        2.创建一个Get请求 ,在括号里写上要调用的第三方的接口
        HttpGet get = new HttpGet("http://dongdongshop.gateway.api.com:7002/contentCateGory/getAllContentCateGory");
//        3.使用HttpClient来执行Get请求, 获取response
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(get);
            if (response == null) {
                return;
            }
//            4.判断响应状态码是不是200，获取响应数据实体对象
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                String s = EntityUtils.toString(entity, "UTF-8");
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//            5.处理response
            try {
                if(response != null){
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
//            6.关闭HttpClient
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * post请求
     */
    @Test
    public void demo02() {
//        1.创建一个HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
//        2.创建一个post请求 ,在括号里写上要调用的第三方的接口

        HttpPost post = new HttpPost("http://dongdongshop.gateway.api.com:7002/contentCateGory/addContentCateGory");
        //设置请求参数
        Map map = new HashMap();
        map.put("name", "测试");
        String mapJson = JSONObject.toJSONString(map);
        StringEntity entity = new StringEntity(mapJson, "UTF-8");
        //设置参数格式为
        entity.setContentType("application/json");
        post.setEntity(entity);
//        3.使用HttpClient来执行post请求, 获取response
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(post);
            if (response == null) {
                return;
            }
//            4.判断响应状态码是不是200，获取响应数据实体对象
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity responseEntity = response.getEntity();
                String s = EntityUtils.toString(responseEntity, "UTF-8");
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//            5.处理response
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
//            6.关闭HttpClient
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 工具类HttpUtil
     */
    @Test
    public void demo03() {
        String s = HttpUtil.get("http://dongdongshop.gateway.api.com:7002/contentCateGory/getAllContentCateGory");
        System.out.println(s);
    }

    /**
     * 工具类HttpUtil post请求
     */
    @Test
    public void demo04() {
        Map map = new HashMap();
        map.put("name","测试");
        String s = HttpUtil.post("http://dongdongshop.gateway.api.com:7002/contentCateGory/addContentCateGory",map);
        System.out.println(s);
    }

    /**
     * 工具类HttpUtil post请求
     */
    @Test
    public void demo05() {
        Map map = new HashMap();
        map.put("name","测试");
        String body = HttpRequest.post("http://dongdongshop.gateway.api.com:7002/contentCateGory/addContentCateGory")
                .body(JSONObject.toJSONString(map))
                .execute().body();
        System.out.println(body);
    }

    /**
     *  第三方接口 :http://apis.juhe.cn/fapig/zodiac/query
     */
    @Test
    public void demo06() {
        Map map = new HashMap();
        map.put("keyword","1998");
        map.put("key","5d2fe001dffa62d0c69ddd96f234ecc5");
        String body = HttpUtil.get("https://api.weibo.com/oauth2/authorize",map);
        System.out.println(body);

    }
}
